package cn.piesat.scanning.dao;

import cn.piesat.scanning.dto.DbDmsSchedulerTaskFileRecordDTO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

/**
 *  调度任务执行文件记录表 持久层
 * @author ly
 */
public interface DbDmsSchedulerTaskFileRecordDAO extends JpaRepository<DbDmsSchedulerTaskFileRecordDTO,String> {

    List<DbDmsSchedulerTaskFileRecordDTO> findByTaskId(String taskId);

    /**
     * 根据任务Id和文件名称集合删除数据
     * @param taskId 任务ID
     * @param fileName 文件名称
     */
//    @Modifying(clearAutomatically = true)
//    @Query("delete from DbDmsSchedulerTaskFileRecordDTO s where s.taskId=:taskId and s.fileName in :fileNameList")
    void deleteByTaskIdAndFileName(String taskId, String fileName);

    void deleteAllByTaskIdAndFileNameIn(String taskId,List<String> fileNameList);



    /**
     * 根据任务Id和文件名称查找数据
     * @param taskId 任务ID
     * @param fileName 文件名称
     * @return
     */
    @Query("from DbDmsSchedulerTaskFileRecordDTO s where s.taskId = :taskId and s.fileName = :fileName")
    DbDmsSchedulerTaskFileRecordDTO findByTaskIdAndFileName(@Param("taskId") String taskId,@Param("fileName") String fileName);

    @Query("from DbDmsSchedulerTaskFileRecordDTO s where s.taskId = :taskId and s.fileName in (:fileNames)")
    List<DbDmsSchedulerTaskFileRecordDTO> findByTaskIdAndFileNames(@Param("taskId") String taskId, @Param("fileNames") List<String> fileNames);

    /**
     * 根据任务ID和文件名称查找数据列表
     * @param taskId 任务ID
     * @param fileName 文件名称
     * @return
     */
    @Query("from DbDmsSchedulerTaskFileRecordDTO s where s.taskId = :taskId and s.fileName = :fileName")
    List<DbDmsSchedulerTaskFileRecordDTO> selectByTaskIdAndFileName(@Param("taskId")String taskId, @Param("fileName")String fileName);

    /**
     * 根据任务ID和文件名称列表查询
     * @param taskId 任务ID
     * @param fileNameList 文件名称集合
     * @return
     */
    List<DbDmsSchedulerTaskFileRecordDTO> findByTaskIdAndFileNameIn(String taskId,List<String> fileNameList);

    @Query("select fileName from DbDmsSchedulerTaskFileRecordDTO where taskId = :taskId")
    List<String> findFileNameListByTaskId(String taskId);

    List<DbDmsSchedulerTaskFileRecordDTO> findFileNameListByTaskIdAndStateAndRedoCountLessThan(String taskId, Integer state, Integer redoCount);

    @Query(value = "select filename from db_dms_scheduler_task_file_record where taskid = ?1" ,nativeQuery = true)
    List<String> listByTaskId(String taskId);

    void deleteByTaskPlanIdIn(List<String> taskPlanIds);

    @Query(nativeQuery = true,value = "select * from db_dms_scheduler_task_file_record where TASKID = :taskId and STATE = 2 and FILENAME in (:execFileNameList)")
    List<DbDmsSchedulerTaskFileRecordDTO> findByFileNameList(@Param("execFileNameList")List<String> execFileNameList,@Param("taskId")String taskId);

    @Modifying
    @Query(nativeQuery = true,value = "update db_dms_scheduler_task_file_record set TASK_PLAN_ID =:taskPlanId WHERE TASKID = :taskId AND FILENAME = :fileName")
    void updateTaskPlanIdByTaskIdAndFileName(String taskId, String fileName, String taskPlanId);

    List<DbDmsSchedulerTaskFileRecordDTO> findByTaskPlanIdAndStateNot(String taskPlanId, int state);

    @Modifying
    @Query("update DbDmsSchedulerTaskFileRecordDTO set state = :state, execServer = :execServer, startTime = :startTime where id in (:ids)")
    void updateStateAndExecServerBatch(Integer state, String execServer, Date startTime, List<String> ids);

    @Modifying
    @Query("update DbDmsSchedulerTaskFileRecordDTO set state = :state, endTime = :endTime where id in (:ids)")
    void updateStateBatch(Integer state, Date endTime, List<String> ids);

    @Query(nativeQuery = true,value = "select * from db_dms_scheduler_task_file_record where filename like %?1% and state <?2")
    List<DbDmsSchedulerTaskFileRecordDTO> findByFileNameLikeAndStateLessThan(String fileDateBei, int state);

    @Modifying
    @Query("delete from DbDmsSchedulerTaskFileRecordDTO where fileName like %?1% and taskPlanId is not null")
    void deleteByFileNameLikeAndTaskPlanIdNotNull(String fileDateBei);

    void deleteByFileNameIn(List<String> l0FileNameList);
}
